package grape.common.tools;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImageTool {

	public static String IMAGE_TYPE_GIF = "gif"; // 图形交换格式
	public static String IMAGE_TYPE_JPG = "jpg"; // 联合照片专家组
	public static String IMAGE_TYPE_JPEG = "jpeg"; // 联合照片专家组
	public static String IMAGE_TYPE_BMP = "bmp"; // 英文Bitmap（位图）的简写，它是Windows操作系统中的标准图像文件格式
	public static String IMAGE_TYPE_PNG = "png"; // 可移植网络图形

	/**
	 *
	 * @param byteArrayInputStream
	 * @return
	 * @throws IOException
	 */
	public final static BufferedImage inputStreamToBufferedImage(ByteArrayInputStream byteArrayInputStream) throws IOException {
		return ImageIO.read(byteArrayInputStream);
	}

	/**
	 *
	 * @param bufferedImage
	 * @param formatName 如：png
	 * @return
	 * @throws IOException
	 */
	public final static InputStream bufferedImageToInputStream(BufferedImage bufferedImage,String formatName) throws IOException {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ImageIO.write(bufferedImage, formatName, os);
		InputStream is = new ByteArrayInputStream(os.toByteArray());
		return is;
	}
}